Skip to content

fix(cli): refactor --override-file rejects files without a valid environments array - #2201

Open
sai-ray wants to merge 1 commit into
mainfrom
sai/fix-override-file-validation
Open

sai-ray wants to merge 1 commit into
mainfrom
sai/fix-override-file-validation

Conversation

@sai-ray

@sai-ray sai-ray commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #2200

cdk refactor --override-file reads a JSON file that tells the CLI how to resolve ambiguous resource moves. The check that should reject a malformed file is always true, so its error never fires. This came in with #679.

The README example also uses a mappings key instead of environments, so copying it gives a broken file.

This PR:

  • rejects the file unless environments is an array and every entry has a resources object. Entries are checked because an entry without resources crashes the same function and does nothing anyway.
  • changes the README example to use environments.

Before and after

Each example runs cdk refactor --unstable=refactor --override-file=overrides.json.

  1. No environments key (the old README example)

Input:

{
  "mappings": [
    {
      "account": "123456789012",
      "region": "us-east-1",
      "resources": {
        "Stack2.OldName": "Stack2.NewName"
      }
    }
  ]
}

Output:

  • before
mappingGroups is not iterable
mappingGroups is not iterable
  • after
Expected an 'environments' array whose entries each have a 'resources' object
Expected an 'environments' array whose entries each have a 'resources' object
  1. An empty environments value

Input:

{ "environments": "" }

Output

  • before:

No error about the file. The CLI runs synth and carries on with the refactor.

  • after:
Expected an 'environments' array whose entries each have a 'resources' object
Expected an 'environments' array whose entries each have a 'resources' object
  1. An entry without resources

Input:

{
  "environments": [
    {
      "account": "123456789012",
      "region": "us-east-1"
    }
  ]
}

Output

  • before:
Cannot convert undefined or null to object
Cannot convert undefined or null to object
  • after:
Expected an 'environments' array whose entries each have a 'resources' object
Expected an 'environments' array whose entries each have a 'resources' object

The README example works as written.

Testing

  • Added three cases to the refusals tests in packages/aws-cdk/test/commands/refactor.test.ts.
  • The new IO snapshots show the CLI prints only the error, so the file is rejected before synth.

Checklist

  • This change contains a major version upgrade for a dependency and I confirm all breaking changes are addressed
    • Release notes for the new version:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@sai-ray
sai-ray requested a review from a team December 4, 2026 00:01
@sai-ray
sai-ray deployed to automation December 4, 2026 00:01 — with GitHub Actions Active
@sai-ray sai-ray changed the title chore: midwork fix(cli): refactor --override-file rejects files without a valid environments array Dec 4, 2026
@github-actions github-actions Bot added the p2 label Dec 4, 2026
@maintainer-for-aws

Copy link
Copy Markdown

Automated review

A maintainer will still review this — treat the notes below as a starting point.

📌 Commit 74b4fbc

This PR fixes the inverted check in parseMappingGroups that reads files passed to cdk refactor --override-file. Before, a missing or non-array environments value was passed straight on and crashed later with a TypeError. Now the file is rejected with MissingEnvironmentsArray unless environments is an array and every entry has a resources object. The README example now uses the environments key, and three CLI tests with snapshots show the file is rejected before synth.

The fix is small and correct, and it closes both crash paths. The only behavior change is that {"environments": ""}, which used to be accepted silently, is now an error, as intended. I found one optional suggestion about how specific the new error message is. Nothing blocks merge.

Open findings: 🔴 0 blocking · 🟡 0 recommended · ⚪ 1 optional.

Files with findings (1)
File Findings
packages/@aws-cdk/toolkit-lib/lib/actions/refactor/private/mapping-helpers.ts ⚪ 1

Generated automatically. React 👍 or 👎 to tell us whether this review helped, so we can improve these reviews.

@maintainer-for-aws maintainer-for-aws Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the review summary comment for the overview; the notes below are inline.

Comment on lines +32 to +33
if (!Array.isArray(environments) || !environments.every((group) => isObject(group?.resources))) {
throw new ToolkitError('MissingEnvironmentsArray', "Expected an 'environments' array whose entries each have a 'resources' object");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Optional — One error message, Expected an 'environments' array whose entries each have a 'resources' object, now covers every kind of bad input: a missing key, a value that isn't an array, and a bad entry at any position. It never says what was actually found. A user with several environment entries can't tell which one failed. The repo's error-handling guidance asks messages to include the wrong value.

Suggested change: Split the check into two branches. If environments is not an array, say what was found instead, such as its type or the file's top-level keys. If an entry has no resources object, find it with findIndex and report its index plus its account/region. Start both messages with Expected an 'environments' array so the new tests still match, then regenerate the three new .ndjson snapshots.

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@sai-ray
sai-ray deployed to no-approval December 4, 2026 00:12 — with GitHub Actions Active
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.56%. Comparing base (d71c502) to head (74b4fbc).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2201      +/-   ##
==========================================
+ Coverage   92.53%   92.56%   +0.03%     
==========================================
  Files          81       81              
  Lines       13238    13238              
  Branches     1917     1917              
==========================================
+ Hits        12250    12254       +4     
+ Misses        950      947       -3     
+ Partials       38       37       -1     
Flag Coverage Δ
suite.unit 92.56% <ø> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active
@sai-ray
sai-ray deployed to run-tests December 4, 2026 00:17 — with GitHub Actions Active

This branch was successfully deployed

3 active deployments
run-tests — 74b4fbcd Deployed Dec 4, 2026 by sai-ray via integ_init-templates (init-typescript-app, 24) #7843
no-approval — 74b4fbcd Deployed Dec 4, 2026 by sai-ray via prepare #7843
automation — 74b4fbcd Deployed Dec 4, 2026 by sai-ray via Set AutoQueue on PR #2201 #4076
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(refactor): parseMappingGroups uses inverted condition, making MissingEnvironmentsArray unreachable

2 participants